#define _CRT_SECURE_NO_WARNINGS 1
#include"Sort.h"
#include"SqStack.h"

void PrintArray(int* a, int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
}
void Swap(int* p1, int* p2)
{
	int tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}

void InsertSort(int* a, int n)
{
	for (int i = 1; i < n; i++)
	{
		int end = i - 1;//有序序列最后一个数据的位置
		int tmp = a[end + 1];//保存要插入的数据,将end后面的一个数据插入
		while (end >= 0)
		{
			if (tmp < a[end])
			{
				a[end + 1] = a[end];//将数据后移
				end--;
			}
			else
			{
				break;
			}
		}
		a[end + 1] = tmp;//找要从插入数据的位置，将数据插入到end后一个位置
	}
}
void ShellSort(int* a, int n)
{
	int gap = n;//两个数据之间的间隔
	while (gap > 1)
	{
		gap /= 2;
		for (int j = 0; j < gap; j++)//j为每一个组的第一个元素的位置
		{
			for (int i = j; i < n - gap; i = i + gap)//i每次跳动gap个距离，对一个组进行排序
			{
				int end = i;//一组有序数据中，最后一个数据的位置
				int tmp = a[end + gap];//表示待插入到这组有序数据的下一个值
				while (end >= 0)
				{
					if (tmp < a[end])
					{//待插入数据小于最后一个数据
						a[end + gap] = a[end];//将最后一个数据往后覆盖
						end = end - gap;//更新end的位置
					}
					else
					{
						break;
					}
				}
				a[end + gap] = tmp;//插入数据
			}//end_for(i)
		}//end_for(j)
	}//end_while (gap > 1)
}

//选择排序
void SelectSort(int* a, int n)
{

	int left = 0, right = n - 1;
	int mini = 0, maxi = 0;
	while (left < right)
	{
		mini = maxi = left;//默认最左边的数据是最大值也是最最小值
		for (int i = left + 1; i <= right; i++)//寻找最小值和最大值的下标
		{
			if (a[mini] > a[i])//更新最小值的下标
			{
				mini = i;
			}
			if (a[maxi] < a[i])//更新最大值的下标
			{
				maxi = i;
			}
		}
		//找到未排序好序列中的最大值和最下值下标
		Swap(&a[mini], &a[left]);//最小值和最左边的数据交换位置
		if (left == maxi)//当最大值和最左边数据相同的时候，更新最大值下标
		{
			maxi = mini;//最小值和最左边交换后，此时最大值变到了，mini的位置
		}
		Swap(&a[maxi], &a[right]);//最大值和最右边的数据交换位置
		//一次遍历结束，更新最左边和最右边的位置
		left++;
		right--;
	}
}

//堆排序
void HeapSort(int* a, int n)
{
	//用向下调整建立大根堆
	for (int i = (n - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDown(a, n, i);
	}
	//将堆顶的值一步一步的往后移动
	while (n)
	{
		Swap(&a[0], &a[n - 1]);
		n--;
		AdjustDown(a, n, 0);
	}
}
void AdjustDown(int* a, int n, int root)
{
	//排升序，建立大根堆
	int parent = root;
	int child = 2 * parent + 1;//找到root的左孩子,默认左孩子是最大的
	while (child < n)
	{
		if (child + 1 < n && a[child + 1] > a[child])//存在右孩子，且右孩子大于左孩子值
		{
			child++;//更新孩子下标值
		}
		if (a[child] > a[parent])//当孩子的值大于父亲结点的时候，交换两个位置的值
		{
			Swap(&a[child], &a[parent]);
		}
		//比较完毕一次，更新父亲与孩子的位置关系
		parent = child;
		child = 2 * parent + 1;
	}
}

// 冒泡排序
void BubbleSort(int* a, int n)
{
	// //实现j+1趟的时候，就会有j+1个值下沉到底部，当n-1都下沉的时候，最轻的也上浮到了水面
	// for (int j = 0; j < n - 1; j++)
	// {
		// for (int i = 1; i < n-j; i++)//一趟冒泡排序
		// {
			// if (a[i - 1] > a[i])//最大值下沉
			// {
				// Swap(&a[i - 1], &a[i]);
			// }
		// }
	// }

	//for (int i = 0; i < n - 1; ++i)
	//{
	//	bool flag = false;
	//	for (int j = 0; j < n - 1 - i; ++j)
	//	{
	//		if (a[j] > a[j + 1])
	//		{
	//			std::swap(a[j], a[j + 1]);	
	//			flag = true;
	//		}
	//	}
	//	if (flag)
	//	{
	//		break;
	//	}
	//}
	
	int exchange = n;
	while (exchange)
	{
		int bound = exchange;
		exchange = 0;
		for (int i = 0; i < bound -1 ; ++i)
		{
			if (a[i] > a[i + 1])
			{
				std::swap(a[i], a[i + 1]);
				exchange = i + 1;
			}
		}
	}
}

//void QuickSort(int* a, int left, int right)
//{
//	//递归结束条件
//	if (left >= right)return;
//	//参数声明
//	int begin = left;//保存最左边区间的值
//	int end = right;//保存最右边区间的值
//	int key = left;//key为关键位置的值
//	while (left < right)//当left==right相等的时候就是key将要待的位置
//	{
//		while (left < right&&a[right] >= a[key])//右边找小；left < right防止right跳到left的左边去
//		{
//			right--;
//		}
//		while (left < right&&a[left] <= a[key])//左边找大；left < right防止left跳到right的右边去
//		{
//			left++;
//		}
//		//交换left和right位置的值，将小的值左移，大的值右移
//		Swap(&a[left], &a[right]);
//	}
//	Swap(&a[key], &a[left]);
//	key = left;
//	//此时key将数组分成三个区间，[begin,key-1] key [key+1,end]
//	//对左区间和右区间分别进行快速排序就可以
//	QuickSort(a, begin, key - 1);
//	QuickSort(a, key + 1, end);
//}


//快速排序
// 快速排序递归实现

//三数取中
int GetMidi(int* a, int left, int right)
{
	int mid = (left + right) / 2;
	if (a[left] < a[mid])
	{
		if (a[mid] < a[right])
			return mid;
		else
		{
			if (a[left] > a[right])
				return left;
			else
				return right;
		}
	}
	else
	{
		if (a[mid] > a[right])
			return mid;
		else
		{
			if (a[left] < a[right])
				return left;
			else
				return right;
		}
	}
}

// 快速排序hoare版本
int PartSort1(int* a, int left, int right)
{
	//随机数法
	/*int randi = left + (rand() % (right - left));
	Swap(&a[left], &a[randi]);*/
	
	//三数取中
	int midi = GetMidi(a, left, right);
	if (midi != left)
		Swap(&a[left], &a[midi]);

	int keyi = left;
	while (left < right)
	{
		while (left<right && a[right]>=a[keyi])//右边找小
			right--;
		while (left < right && a[left] <= a[keyi])//左边找大
			left++;
		Swap(&a[left], &a[right]);
	}

	Swap(&a[keyi], &a[left]);
	keyi = left;
	return keyi;
}
// 快速排序挖坑法
int PartSort2(int* a, int left, int right)
{
	//三数取中
	int midi = GetMidi(a, left, right);
	if (midi != left)
		Swap(&a[left], &a[midi]);

	int key = a[left];
	int hole = left;
	while (left < right)
	{
		while (left<right && a[right]>key)
			right--;
		a[hole] = a[right];
		hole = right;

		while (left < right && a[left] < key)
			left++;
		a[hole] = a[left];
		hole = left;
	}
	a[hole] = key;
	return hole;
}
// 快速排序前后指针法
int PartSort3(int* a, int left, int right)
{
	//三数取中
	int midi = GetMidi(a, left, right);
	if (midi != left)
		Swap(&a[left], &a[midi]);

	int pre = left;
	int cur = left + 1;
	while (cur <= right)
	{
		if (a[cur] < a[left])
		{
			if (++pre != cur)
				Swap(&a[pre], &a[cur]);
		}
		cur++;
	}
	Swap(&a[left], &a[pre]);
	return pre;
}
void QuickSort(int* a, int left, int right)
{
	//递归结束条件
	if (left >= right)return;

	//小区间优化
	if (right - left + 1 > 10)
	{
		int keyi = PartSort1(a, left, right);//找到关键位置
		QuickSort(a, left, keyi - 1);
		QuickSort(a, keyi + 1, right);
	}
	else
	{
		InsertSort(a + left, right - left + 1);
	}
}

//快速排序非递归
void QuickSortNonR(int* a, int left, int right)
{
	//三数取中
	int midi = GetMidi(a, left, right);
	if (midi != left)
		Swap(&a[left], &a[midi]);

	Stack S; StackInit(&S);//创建和初始化一个栈
	StackPush(&S, right);
	StackPush(&S, left);
	while (!StackEmpty(&S))
	{
		int begin = StackTop(&S); StackPop(&S);
		int end = StackTop(&S); StackPop(&S);
		int keyi = PartSort3(a, begin, end);

		//子区间存在入栈 [begin,keyi-1] keyi [keyi+1,end]
		if (keyi + 1 < end)
		{
			StackPush(&S, end);
			StackPush(&S, keyi+1);
		}
		if (left < keyi - 1)
		{
			StackPush(&S, keyi-1);
			StackPush(&S, left);
		}
	}
	StackDestroy(&S);
}

//三路快速排序递归算法
void QuickThreeWays(int* a, int left, int right)
{
	if (left >= right)return;

	//三数取中
	int midi = GetMidi(a, left, right);
	if (midi != left)
		Swap(&a[left], &a[midi]);

	int keyi = left;
	int Lt = left + 1;
	int i = left + 1;
	int Rt = right + 1;

	while (i < Rt)
	{
		if (a[i] > a[keyi])
			Swap(&a[i], &a[--Rt]);
		else if (a[i] == a[keyi])
			i++;
		else
		{
			Swap(&a[i], &a[Lt]);
			i++;
			Lt++;
		}
	}
	Swap(&a[keyi], &a[Lt - 1]);
	QuickThreeWays(a, left, Lt - 1);
	QuickThreeWays(a, Rt, right);
}

// 归并排序递归实现
void _MergeSort(int* a, int left,int right,int *tmp)
{
	if (left >= right)return;

	//将区间分成两个子序列，并将这两个子序列变成有序
	int mid = (left + right) / 2;
	_MergeSort(a, left, mid,tmp);
	_MergeSort(a, mid + 1, right, tmp);

	//将这两个子序列合并成一个有序的序列，借助而外的空间
	int i = left;
	int begin1 = left;
	int end1 = mid;
	int begin2 = mid + 1;
	int end2 = right;
	while (begin1 <= end1 && begin2 <= end2)
	{
		if (a[begin1] < a[begin2])
			tmp[i++] = a[begin1++];
		else
			tmp[i++] = a[begin2++];
	}
	while(begin1 <= end1)
		tmp[i++] = a[begin1++];
	while (begin2 <= end2)
		tmp[i++] = a[begin2++];

	//拷贝回原来空间
	memcpy(a + left, tmp + left, sizeof(int) * (right - left + 1));
}
void MergeSort(int* a, int n)
{
	int* tmp = (int*)malloc(sizeof(int)*n);
	if (!tmp)
	{
		perror("malloc fail");
		return;
	}
	_MergeSort(a, 0, n - 1, tmp);
	free(tmp);
}
// 归并排序非递归实现
void MergeSortNonR(int* a, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
	if (!tmp)
	{
		perror("malloc fail");
		return;
	}
	int gap = 1;
	while (gap < n)
	{
		for (int i = 0; i < n; i = i + 2 * gap)
		{
			int begin1 = i;
			int end1 = i + gap - 1;
			int begin2 = i + gap;
			int end2 = i + 2 * gap - 1;
			int j = begin1;

			
			//修正
			if (end1 >= n)
			{
				break;
			}
			else if (begin2 >= n)
			{
				break;
			}
			else if (end2 >= n)
			{
				end2 = n - 1;
			}
			//printf("[%d,%d][%d,%d] ", begin1, end1, begin2, end2);

			while (begin1 <= end1 && begin2 <= end2)
			{
				if (a[begin1] < a[begin2])
					tmp[j++] = a[begin1++];
				else
					tmp[j++] = a[begin2++];
			}
			while (begin1 <= end1)
				tmp[j++] = a[begin1++];
			while (begin2 <= end2)
				tmp[j++] = a[begin2++];
			//拷贝回原来空间
			memcpy(a + i, tmp + i, sizeof(int) * (end2-i+1));
		}
		//printf("\n");
		gap *= 2;
	}
	free(tmp);
}


//计数排序
void CountSort(int* a, int n)
{
	int min = a[0];
	int max = a[0];
	//找到范围range，建立相对映射
	for (int i = 1; i < n; i++)
	{
		max = a[i] > max ? a[i] : max;
		min = a[i] < min ? a[i] : min;
	}
	int range = max - min + 1;
	//开辟相对映射的空间数组
	int* CountA = (int*)malloc(sizeof(int) * range);
	if (CountA == NULL)
	{
		perror("malloc fail");
		return;
	}
	memset(CountA, 0, sizeof(int) * range);//初始化设置为0

	//映射计数
	for (int i = 0; i < n; i++)
	{
		CountA[a[i] - min]++;
	}

	//覆盖排序
	int j = 0;
	for (int i = 0; i < range; i++)
	{
		while (CountA[i]--)
		{
			a[j++] = i + min;
		}
	}

	free(CountA);
}

// 测试排序的性能对比

void TestOP()
{
	srand(time(0));
	const int N = 100000;
	int* a1 = (int*)malloc(sizeof(int) * N);
	int* a2 = (int*)malloc(sizeof(int) * N);
	int* a3 = (int*)malloc(sizeof(int) * N);
	int* a4 = (int*)malloc(sizeof(int) * N);
	int* a5 = (int*)malloc(sizeof(int) * N);
	int* a6 = (int*)malloc(sizeof(int) * N);
	for (int i = 0; i < N; ++i)
	{
		a1[i] = rand();
		a2[i] = a1[i];
		a3[i] = a1[i];
		a4[i] = a1[i];
		a5[i] = a1[i];
		a6[i] = a1[i];
	}
	int begin1 = clock();
	InsertSort(a1, N);
	int end1 = clock();
	int begin2 = clock();
	ShellSort(a2, N);
	int end2 = clock();
	int begin3 = clock();
	SelectSort(a3, N);
	int end3 = clock();
	int begin4 = clock();
	HeapSort(a4, N);
	int end4 = clock();
	int begin5 = clock();
	QuickSort(a5, 0, N - 1);
	int end5 = clock();
	int begin6 = clock();
	MergeSort(a6, N);
	int end6 = clock();
	printf("InsertSort:%d\n", end1 - begin1);
	printf("ShellSort:%d\n", end2 - begin2);
	printf("SelectSort:%d\n", end3 - begin3);
	printf("HeapSort:%d\n", end4 - begin4);
	printf("QuickSort:%d\n", end5 - begin5);
	printf("MergeSort:%d\n", end6 - begin6);
	free(a1);
	free(a2);
	free(a3);
	free(a4);
	free(a5);
	free(a6);
}